home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GSDPS1.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  4KB  |  130 lines

  1. /* Copyright (C) 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gsdps1.c */
  20. /* Display PostScript graphics additions for Ghostscript library */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gxfixed.h"
  24. #include "gxmatrix.h"
  25. #include "gzpath.h"
  26. #include "gzstate.h"
  27.  
  28. /* ------ Graphics state ------ */
  29.  
  30. /* Set the bounding box for the current path. */
  31. int
  32. gs_setbbox(gs_state *pgs, floatp llx, floatp lly, floatp urx, floatp ury)
  33. {    gs_rect ubox, dbox;
  34.     gs_fixed_rect bbox;
  35.     gx_path *ppath = pgs->path;
  36.     int code;
  37.     if ( llx > urx || lly > ury )
  38.         return_error(gs_error_rangecheck);
  39.     /* Transform box to device coordinates. */
  40.     ubox.p.x = llx;
  41.     ubox.p.y = lly;
  42.     ubox.q.x = urx;
  43.     ubox.q.y = ury;
  44.     if ( (code = gs_bbox_transform(&ubox, &ctm_only(pgs), &dbox)) < 0 )
  45.         return code;
  46.     if ( gx_path_bbox(ppath, &bbox) >= 0 )
  47.     {    /* Take the union of the bboxes. */
  48.         ppath->bbox.p.x = min(bbox.p.x, float2fixed(dbox.p.x));
  49.         ppath->bbox.p.y = min(bbox.p.y, float2fixed(dbox.p.y));
  50.         ppath->bbox.q.x = max(bbox.q.x, float2fixed(dbox.q.x));
  51.         ppath->bbox.q.y = max(bbox.q.y, float2fixed(dbox.q.y));
  52.     }
  53.     else        /* empty path */
  54.     {    /* Just set the bbox. */
  55.         ppath->bbox.p.x = float2fixed(dbox.p.x);
  56.         ppath->bbox.p.y = float2fixed(dbox.p.y);
  57.         ppath->bbox.q.x = float2fixed(dbox.q.x);
  58.         ppath->bbox.q.y = float2fixed(dbox.q.y);
  59.         ppath->bbox_set = 1;
  60.     }
  61.     return 0;
  62. }
  63.  
  64. /* ------ Rectangles ------ */
  65.  
  66. /* Append a list of rectangles to a path. */
  67. int
  68. gs_rectappend(gs_state *pgs, const gs_rect *pr, uint count)
  69. {    for ( ; count != 0; count--, pr++ )
  70.        {    floatp px = pr->p.x, py = pr->p.y, qx = pr->q.x, qy = pr->q.y;
  71.         int code;
  72.         /* Ensure counter-clockwise drawing. */
  73.         if ( (qx >= px) != (qy >= py) )
  74.             qx = px, px = pr->p.x;    /* swap x values */
  75.         if ( (code = gs_moveto(pgs, px, py)) < 0 ||
  76.              (code = gs_lineto(pgs, qx, py)) < 0 ||
  77.              (code = gs_lineto(pgs, qx, qy)) < 0 ||
  78.              (code = gs_lineto(pgs, px, qy)) < 0 ||
  79.              (code = gs_closepath(pgs)) < 0
  80.            )
  81.             return code;
  82.        }
  83.     return 0;
  84. }
  85.  
  86. /* Clip to a list of rectangles. */
  87. int
  88. gs_rectclip(gs_state *pgs, const gs_rect *pr, uint count)
  89. {    int code;
  90.     if ( (code = gs_newpath(pgs)) < 0 ||
  91.          (code = gs_rectappend(pgs, pr, count)) < 0 ||
  92.          (code = gs_clip(pgs)) < 0 ||
  93.          (code = gs_newpath(pgs)) < 0
  94.        )
  95.         return code;
  96.     return 0;
  97. }
  98.  
  99. /* Fill a list of rectangles. */
  100. /* (We could do this a lot more efficiently.) */
  101. int
  102. gs_rectfill(gs_state *pgs, const gs_rect *pr, uint count)
  103. {    int code;
  104.     if ( (code = gs_gsave(pgs)) < 0 ) return code;
  105.     if ( (code = gs_newpath(pgs)) < 0 ||
  106.          (code = gs_rectappend(pgs, pr, count)) < 0 ||
  107.          (code = gs_fill(pgs)) < 0
  108.        )
  109.         ;
  110.     gs_grestore(pgs);
  111.     return code;
  112. }
  113.  
  114. /* Stroke a list of rectangles. */
  115. /* (We could do this a lot more efficiently.) */
  116. int
  117. gs_rectstroke(gs_state *pgs, const gs_rect *pr, uint count,
  118.   const gs_matrix *pmat)
  119. {    int code;
  120.     if ( (code = gs_gsave(pgs)) < 0 ) return code;
  121.     if ( (code = gs_newpath(pgs)) < 0 ||
  122.          (code = gs_rectappend(pgs, pr, count)) < 0 ||
  123.          (pmat != NULL && (code = gs_concat(pgs, pmat)) < 0) ||
  124.          (code = gs_stroke(pgs)) < 0
  125.        )
  126.         ;
  127.     gs_grestore(pgs);
  128.     return code;
  129. }
  130.